/*
 * Copyright (c) 2008-2010, Petr Panteleyev
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 *    1. Redistributions of source code must retain the above copyright notice,
 *       this list of conditions and the following disclaimer.
 *    2. Redistributions in binary form must reproduce the above copyright notice,
 *       this list of conditions and the following disclaimer in the documentation
 *       and/or other materials provided with the distribution.
 *    3. The name of the author may not be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package org.panteleyev.gclient;

import java.awt.BorderLayout;
import java.awt.Component;
import java.util.EventObject;

import javax.swing.Box;
import javax.swing.JMenu;
import javax.swing.JTabbedPane;
import javax.swing.JToolBar;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import org.panteleyev.blogclient.BloggerFrame;
import org.panteleyev.blogclient.Icon;
import org.panteleyev.blogclient.Options;
import org.panteleyev.swing.MenuProvider;
import org.panteleyev.swing.SwingFactory;
import org.panteleyev.swing.ToolBarProvider;
import org.panteleyev.utilities.Updatable;
import org.panteleyev.utilities.events.AppEventListener;

/**
 *
 * @author  Petr Panteleyev
 */
final class GPicasaGalleryFrame extends BloggerFrame<GBUserProfile> implements Updatable {
    private JTabbedPane     tabs = new JTabbedPane();
    private JToolBar        panelToolBar;

    public GPicasaGalleryFrame(GBUserProfile profile) {
        super(profile);

        initComponents();

        addAppEventListener(AlbumFhotosUpdateEvent.class, new AppEventListener() {
            @Override
            public void eventReceived(EventObject event) {
                onAlbumUpdatedEvent(((AlbumFhotosUpdateEvent)event).getAlbum());
            }
        });

        Options.getBounds(this);
    }

    private void initComponents() {
        setTitle(L10N.PGF_TITLE.s());
        setIconImage(Icon.PICASA.I().getImage());
        
        getContentPane().setLayout(new BorderLayout());

        PicasaGalleryPanel panel = new PicasaGalleryPanel(this, getProfile());
        tabs.add("Albums", panel);

        getContentPane().add(
            SwingFactory.createToolBarPanel(
                panelToolBar = SwingFactory.createToolBar(false, true,
                    panel.getToolBarActions()
                ),
                Box.createHorizontalStrut(5),
                createProfileToolBar(false, true),
                Box.createHorizontalGlue(),
                SwingFactory.createToolBar(false, true, getUpdateAction()),
                Box.createHorizontalStrut(5),
                getBusyLabel(),
                Box.createHorizontalStrut(5)
            ), BorderLayout.NORTH
        );

        getContentPane().add(tabs, BorderLayout.CENTER);

        tabs.addChangeListener(new ChangeListener() {
            @Override
            public void stateChanged(ChangeEvent evt) {
                tabbedPaneStateChanged(evt);
            }
        });

        createMenuBar(
            createFileMenu(),
            panel.getMenu(),
            createProfileMenu(),
            createToolsMenu(),
            createWindowMenu(),
            createHelpMenu()
        );

    }

    private void tabbedPaneStateChanged(ChangeEvent evt) {
        Component comp = tabs.getSelectedComponent();

        if (comp instanceof MenuProvider) {
            Component c = getJMenuBar().getComponent(1);
            c.setVisible(false);
            JMenu menu = ((MenuProvider)comp).getMenu();
            menu.setVisible(true);
            getJMenuBar().remove(1);
            getJMenuBar().add(menu, 1);
        }

        if (comp instanceof ToolBarProvider) {
            panelToolBar.removeAll();
            SwingFactory.fill(panelToolBar, ((ToolBarProvider)comp).getToolBarActions());
        }
    }

    @Override
    public void update() {
        Component comp = tabs.getSelectedComponent();
        if (comp instanceof Updatable) {
            ((Updatable)comp).update();
        }
    }

    public void onOpenAlbum(GAlbum album) {
        // check if this set is already open
        for (Component comp : tabs.getComponents()) {
            if (comp instanceof PicasaAlbumPanel) {
                if (((PicasaAlbumPanel)comp).getAlbum() == album) {
                    tabs.setSelectedComponent(comp);
                    return;
                }
            }
        }

        PicasaAlbumPanel panel = new PicasaAlbumPanel(this, getProfile(), album);
        tabs.add(album.getTitle(), panel);
        tabs.setSelectedComponent(panel);
    }

    private void onAlbumUpdatedEvent(GAlbum album) {
        for (Component comp : tabs.getComponents()) {
            if (comp instanceof PicasaAlbumPanel) {
                if (((PicasaAlbumPanel)comp).getAlbum() == album) {
                    ((PicasaAlbumPanel)comp).update();
                    return;
                }
            }
        }

    }

}
